package com.example.forum.controller;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.config.AppConfig;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.Article;
import com.example.forum.model.ArticleReply;
import com.example.forum.model.User;
import com.example.forum.services.IArticleReplyService;
import com.example.forum.services.IArticleService;
import com.sun.istack.internal.NotNull;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "回帖接口")
@Slf4j
@RestController
@RequestMapping("/reply")
public class ArticleReplyController {
    @Resource
    private IArticleService articleService;

    @Resource
    private IArticleReplyService articleReplyService;

    @ApiOperation("回复帖子")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @ApiParam("帖子id") @RequestParam("articleId") @NotNull Long articleId,
                            @ApiParam("帖子内容") @RequestParam("content") @NotNull String content){
        User user=AppConfig.getUser(request);
        if(user.getState()==1){
            log.warn(ResultCode.FAILED_USER_BANNED.toString());
            return AppResult.fail(ResultCode.FAILED_USER_BANNED);
        }
        Article article=articleService.selectById(articleId);
        if(article==null||article.getDeleteState()==1){
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            return AppResult.fail(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        if(article.getState()==1){
            log.warn(ResultCode.FAILED_ARTICLE_BANNED.toString());
            return AppResult.fail(ResultCode.FAILED_ARTICLE_BANNED);
        }
        ArticleReply updateArticleReply=new ArticleReply();
        updateArticleReply.setArticleId(articleId);
        updateArticleReply.setContent(content);
        updateArticleReply.setPostUserId(user.getId());
        articleReplyService.create(updateArticleReply);
        return AppResult.success();
    }

    @ApiOperation("获取回复帖子列表")
    @GetMapping("/getReplies")
    public AppResult<List<ArticleReply>> getRepliesById(@ApiParam("帖子id") @RequestParam("articleId") @NotNull Long articleId){
        Article article=articleService.selectById(articleId);
        if(article==null||article.getDeleteState()==1){
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            return AppResult.fail(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        if(article.getState()==1){
            log.warn(ResultCode.FAILED_ARTICLE_BANNED.toString());
            return AppResult.fail(ResultCode.FAILED_ARTICLE_BANNED);
        }
        List<ArticleReply> articleReplies = articleReplyService.selectByArticleId(articleId);
        return AppResult.success(articleReplies);
    }
}
